<template>
  <div>
    <el-dialog
        title="添加/修改"
        :visible.sync="dialogFormVisible"
        @close="clear">
      <el-form v-model="form" style="text-align: left;" ref="dataForm">
        <el-form-item label="监测点ID" :label-width="formLabelWidth" prop="ID">
          <el-input  v-model="form.id" type="text" :change="check_num_id()"></el-input>
        </el-form-item>
        <el-form-item label="监测点名" :label-width="formLabelWidth" prop="Name">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="监测点负责人" :label-width="formLabelWidth" prop="HandPeople">
          <el-input v-model="form.handPeople" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="负责人电话" :label-width="formLabelWidth" prop="Tel">
          <el-input v-model="form.tel" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="选择地址" :label-width="formLabelWidth" prop="PointId">
          <el-select v-model="form.pointId" placeholder="请选择地址">
            <el-option v-for="i in pointList" :key="i.id" v-bind:label="i.addressName" v-bind:value="i.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth" prop="Status">
          <el-select v-model="form.status" >
            <el-option label="正常" value="正常"></el-option>
            <el-option label="警告" value="警告"></el-option>
            <el-option label="异常" value="异常"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" v-on:click="onSubmit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'UploadForm',
  data () {
    return {
      dialogFormVisible: false,
      form: {
        id:'',
        name:'',
        handPeople:'',
        tel:'',
        pointId:'',
        status:'正常',
        grade:'Green',
      },
      pointList:[],

      formLabelWidth: '100px'
    }
  },
  methods: {
    update(data){
      this.getPointList();
      this.form.id=data.id;
      this.form.name=data.name;
      this.form.handPeople=data.handPeople
      this.form.tel=data.tel;
      this.form.status=data.status;
      this.form.grade=data.grade;
      this.form.pointId=data.pointId;
      console.log(this.form)
      this.dialogFormVisible=true
    },

    open(){
      this.getPointList();
      this.getId();
      this.dialogFormVisible=true;
    },
    getId(){
      this.$axios.get('/maxId').then(res=>{
        this.form.id=res.data+1;
      })
    },
    clear () {
      this.form = {
        id: '',
        name:'',
        handPeople: '',
        tel:'',
        pointId: '',
        status: '正常',
        grade:'Green'
      }
    },
    getPointList(){
      this.$axios.get('/maps').then(res=>{
        this.pointList=res.data;
      })
    },
    check_num_id: function(){
      var price = '' + this.form.id;
      price = price.replace(/[^\d]/g, '');
      this.form.id = price;
    },
    onSubmit(){
      this.$axios.post('/addSection',{
        id:this.form.id,
        name:this.form.name,
        pointId:this.form.pointId,
        handPeople:this.form.handPeople,
        tel:this.form.tel,
        status:this.form.status,
        grade:this.form.grade
      })
      .then(res=>{
        if(res.data.code===200){
          this.$emit('download');
          this.$message({
            showClose: true,
            message:'上传成功',
            type:'success'
          });
          this.dialogFormVisible=false;
        }
      })
    }

  }
}
</script>

<style scoped>
.el-icon-circle-plus-outline {
  margin: 50px 0 0 20px;
  font-size: 100px;
  float: left;
  cursor: pointer;
}
</style>
